/**
 * TMP显示
 * */


//获取一条数据
function getOne(){
    if (code == '' || code == undefined) {
        msgPop({msg: '码值错误'});
        return
    }
    var formData = {
        code: code, h5code: h5code,
    };
    console.log(formData);
    $.ajax({
        type: apis.tmpOne.method,
        url: apis.tmpOne.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                msgPop({msg: res.msg});
                return
            }
            var d = res.data;
            var h = '', i;
            $('#title').html(d.title);
            $('#show_code').html(d.show_code);
            $('#visit_total').html(d.visit_total);
            $('#scene_name').html(d.scene_name);
            $('#describe').html(d.describe);
            if (d.isfollow > 0) {
                $('#followBtn').html('已关注');
            }
            if (d.attrList.length > 0) {
                h = '';
                for (i = 0; i < d.attrList.length; i ++) {
                    h += '<tr>';
                    h += '<td class="tl">' + d.attrList[i].attr_key + '</td>';
                    h += '<td class="tr">' + d.attrList[i].attr_val + '</td>';
                    h += '</tr>';
                }
                $('#attrList').html(h);
            }
            if (d.ad.thumb != '') {
                $('#advert').html('<img src="' + d.ad.thumb + '" width="100%"/>');
                //setTimeout(function(){
                //    setAdUse(d.ad);
                //}, 5000);
            }
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//广告设置
function setAdUse(e){
    var formData = {
        id: e.use_id, ad_id: e.ad_id,
        code: code, h5code: h5code,
    };
    $.ajax({
        type: apis.tmpAdUse.method,
        url: apis.tmpAdUse.url,
        data: formData,
        success: function (res) {
            console.log(res);
            //if (res.code != 0) {
            //    msgPop({msg: res.msg});
            //    return
            //}
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//获取评论
function replyList(){
    var formData = {
        code: code,
    };
    //console.log(formData);
    $.ajax({
        type: apis.tmpReplyIndex.method,
        url: apis.tmpReplyIndex.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                //msgPop({msg: res.msg});
                return
            }
            replyHtml(res.data);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}
//评论HTML
function replyHtml(d){
    var h = '', i, id, addDates, sender;
    for (i = 0; i < d.length; i ++) {
        id = d[i].id;
        sender = d[i].pub;
        addDates = d[i].addtime.split(' ');
        h += '<table>';
        h += '<tr>';
        h += '<td class="pl-l">';
        h += '<img src="/h5/imgs/uhead.png"/>';
        //h += '<div>' + strSuffix(sender, 5) + '</div>';
        h += '<div>' + sender + '</div>';
        h += '</td>';
        h += '<td class="pl-r">';
        h += '<div>';
        h += '<span style="color: #ccc;">' + addDates[0] + '</span>';
        h += '</div>';
        h += '<div style="width: ' + (cw * 1 - 80) + 'px; max-height: 50px; word-wrap: break-word; overflow: hidden;">' + d[i].cnt + '</div>';
        h += '</td>';
        h += '</tr>';
        h += '</table>';
        h += '<div class="div-hr"></div>';
    }
    $('#mbody').html(h);
}


//评论新增
function replyAdd(){
    if (h5code == '') {
        msgPop({msg: '没有登录'});
        setTimeout(function(){
            window.location.href = '/h5/login.html?code=' + code;
        }, 1000);
        return
    }
    var cnt = $('#pl-txt').val();
    if (cnt == '') {
        msgPop({msg: '评论没有填写'});
        return
    }
    var formData = {
        h5code: h5code, code: code,
        cnt: cnt,
    };
    $.ajax({
        type: apis.tmpReplyAdd.method,
        url: apis.tmpReplyAdd.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                msgPop({msg: res.msg}); return
            }
            msgPop({msg: '发布成功'});
            replyList();
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//设置关注
function setFollow(){
    if (h5code == '') {
        msgPop({msg: '没有登录'});
        setTimeout(function(){
            window.location.href = '/h5/login.html?code=' + code;
        }, 1000);
        return
    }
    var formData = {
        h5code: h5code, code: code,
    };
    console.log(formData);
    $.ajax({
        type: apis.tmpFollowAdd.method,
        url: apis.tmpFollowAdd.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                msgPop({msg: res.msg});
                return
            }
            msgPop({msg: '操作成功'});
            setTimeout(function(){
                location.href = '';
            }, 500);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}